package com.hk.Mgr.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.ParameterReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.Parameter;
import com.hk.service.order.IParameter;

/**
 * @author lemon 用户实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/parameterCtrl")
public class ParameterCtrl extends BaseCtrl {

	@Autowired
	private IParameter parmService;


	/**
	 * 根据名称获取参数
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getParameter", method = RequestMethod.GET)
	public ResponseBase<Parameter>  getParameter(String name){
		ResponseBase<Parameter> resp = new ResponseBase<>();
		if(StringUtils.isBlank(name)){
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			resp.setMsg("参数名为空");
		}

		Parameter parameter = parmService.getByName(name);
		if(parameter == null){
			resp.setResultCode(StatusCode.DATA_NOTEXISTS.key());
			resp.setMsg(StatusCode.DATA_NOTEXISTS.desc());
		}
		resp.setResult(parameter);
		return resp;
	}

	/**
	 * 获取参数列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public ResponseBase<List<Parameter>> list(@RequestBody ParameterReq req) {
		ResponseBase<List<Parameter>> resp = new ResponseBase<List<Parameter>>();
		List<Parameter> resList = null;
		String name = req.getName();
		//String flag = req.getFlag();
		logger.info("mgr/parameterCtrl/list name = " + name );
		 
		HqlFilter hqlFilter = new HqlFilter();
		if (StringUtils.isNotEmpty(name)) {
			hqlFilter.addLKFilter("name", name);;
		}
		
		try {

			resList = parmService.findByFilter(hqlFilter, req.getCurPage(), req.getPageSize());
			resp.setResult(resList);
			resp.setSize(resList.size());
			resp.setResultCode(StatusCode.SUCCESS.key());
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
	
	/**
	 * 获取参数列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ResponseBase<Boolean> save(@RequestBody ParameterReq req) {
		ResponseBase<Boolean> resp = new ResponseBase<Boolean>();
		 
		Parameter parm = new Parameter();
		ReqToModelHelper.copyReqValueToModel(req, parm);
		
		Parameter parmExist = parmService.getByName(parm.getName());
		
		if(parmExist != null){
			resp.setMsg("该参数名已存在，保存错误！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}
		
		try {
			parmService.add(parm);
			resp.setResult(true);
			resp.setResultCode(StatusCode.SUCCESS.key());
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
	
	/**
	 * 获取参数列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseBase<Boolean> update(@RequestBody ParameterReq req) {
		ResponseBase<Boolean> resp = new ResponseBase<Boolean>();
		if(req == null || req.getId() == null){
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}
		Parameter parm = new Parameter();
		ReqToModelHelper.copyReqValueToModel(req, parm);
		
		Parameter parmExist = parmService.getByName(parm.getName());

		if( parmExist != null && parmExist.getId()!= null && parmExist.getId().compareTo(parm.getId()) == 1){
			resp.setMsg("该参数名已存在，跟新失败错误！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}

		try {
			parmService.update(parm);
			resp.setResult(true);
			resp.setResultCode(StatusCode.SUCCESS.key());
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

}
